﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Module_FormLogin : PanelControl
{
    UserBusiness ub=new UserBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        UserEntities ue = this.getUser();
        if (ue == null)
        {
            this.login.Visible = true;
            this.logout.Visible = false;
            
        }
        else if(ue!=null)
        {
            //if (ue._roleID != "1")
            //    this.an.Visible = false;
            //else
            //    this.an.Visible = true;
            this.login.Visible = false;
            this.logout.Visible = true;
            lblUserName.Text = ue._userName;
            lblRole.Text = role(int.Parse(ue._roleID));
        }
    }

    public UserEntities getUser()
    {
        UserEntities ue = (UserEntities)Session["user"];
        return ue;
    }

    public string role(int roleID)
    {
        string tmp = "";
        RoleBusiness rb = new RoleBusiness();
        RoleEntities re = rb.getRole(roleID);
        if (re == null)
            tmp = "Chưa cấp quyền";
        else
            tmp = re._roleName;
        return tmp;
    }
    
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session["user"] = null;
        Response.Redirect("Default.aspx");
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string userName = txtUserName.Text.Trim();
        string pass = Helper.EncodePass(txtPass.Text.Trim());
        UserEntities ue = ub.login(userName, pass);
        if (ue != null)
        {
            Session["user"] = ue;
            BookingEntities be = new BookingEntities();
            be = (BookingEntities)Session["datve"];
            if (be != null)
            {
                if (be._classDi > 0)
                    Response.Redirect("Default.aspx?url=Module/Itinerary.ascx");
                else
                {
                    Session["datve"] = null;
                    Response.Redirect("Default.aspx");
                }
            }
            else
                Response.Redirect("Default.aspx");
        }
        
        //if (ue != null)
        //{
        //    Session["user"] = ue;
        //    Response.Redirect("Default.aspx");
        //}
        else
            Label1.Text = "Tên đăng nhập không tồn tại";
    }
    protected void btnResg_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?url=Module/FormDangKy.ascx");
    }
    public string proFile()
    {
        return "<a href=\"Default.aspx?url=Module/Profile.ascx&userID="+this.getUser()._userID+"\">Thông tin tài khoản</a>";
    }
}